package com.ftg.learn.mapper;

import com.ftg.learn.dto.SalePart;
import com.ftg.learn.entity.SaleEntity;
import com.ftg.learn.mapper.provider.SaleMapperProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * sale表操作
 * @author wangty
 * @date 2022/10/18
 */
@Mapper
public interface SaleMapper {

    /**
     * 查找全部商品信息
     * @return  商品信息表
     */
    @Select("select * from sale")
    List<SaleEntity> showAll();

    /**
     * 模糊查询带一个参数
     * @param name 商品名称
     * @return
     */
    @Select("select * from sale where flag = 0 and name like concat(#{name},'%')")
    List<SaleEntity> likeWithName(@Param("name") String name);

    /**
     * 动态模糊查询
     * @param name 商品名称
     * @param address 商品地址
     * @return
     */
    @SelectProvider(value = SaleMapperProvider.class,method = "likeWithNameAddressSql")
    List<SaleEntity> likeWithNameAddress(@Param("name") String name, @Param("address") String address);

    /**
     * 按照分页模糊查询
     * @param start 起始页数
     * @param limit 一页显示几条
     * @param name 商品名称
     * @param address 商品地址
     * @return
     */
    @SelectProvider(value = SaleMapperProvider.class,method = "pageWithNameAddressSql")
    List<SaleEntity> pageWithNameAddress(@Param("start") int start, @Param("limit") Integer limit, @Param("name") String name, @Param("address")String address);

    /**
     * 分页查询
     * @param name 商品名称
     * @param address 商品地点
     * @return
     */
    @SelectProvider(value = SaleMapperProvider.class,method = "pageWithNameAddressCountSql")
    int pageWithNameAddressCount( @Param("name") String name, @Param("address")String address);

    /**
     * 按照实例进行添加
     * @param saleEntity 实例
     * @return
     */
    @Insert("insert into sale(name, address, fid, unit, price, type) values(" +
            "#{name},#{address},#{fid},#{unit},#{price},#{type})")
    int addEntity(SaleEntity saleEntity);

    /**
     * 按照dto进行添加
     * @param salePart 部分重要属性
     * @return
     */
    @Insert("insert into sale(name, address, price) values(" +
            "#{name},#{address},#{price})")
    int addDto(SalePart salePart);

    /**
     * 按照实例进行修改
     * @param saleEntity
     * @return
     */
    @Update("update sale set name = #{sale.name},address = #{sale.address}," +
            " fid = #{sale.fid},unit = #{sale.unit},price = #{sale.price}," +
            "type = #{sale.type} where id = #{sale.id}")
    int updateSaleEntity(@Param("sale") SaleEntity saleEntity);

    /**
     * 按照主键进行修改
     * @param saleEntity 实例
     * @param id 主键
     * @return
     */
    @Update("update sale set name = #{sale.name},address = #{sale.address}," +
            "fid = #{sale.fid},unit = #{sale.unit},price = #{sale.price}," +
            "type = #{sale.type} where id = #{sale.id}" )
    int updateSaleEntityWithId(@Param("sale") SaleEntity saleEntity, @Param("id") Integer id);

    /**
     * 按照主键进行删除
     * @param id 主键
     * @return
     */
    @Delete("delete from sale where id = #{id}")
    int deleteById(Integer id);

    /**
     * 逻辑删除
     * @param id 主键
     * @param flag 判断
     * @return
     */
    @Delete("delete from sale where id = #{id} and flag = #{flag}")
    int deleteByIdLogic(Integer id, Integer flag);
}

